package cn.itsource.pet.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.LoginContext;
import cn.itsource.basic.util.PageList;
import cn.itsource.pet.domain.Pet;
import cn.itsource.pet.domain.SearchMasterMsg;
import cn.itsource.pet.query.SearchMasterMsgQuery;
import cn.itsource.pet.service.ISearchMasterMsgService;
import cn.itsource.user.domain.Logininfo;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    private ISearchMasterMsgService seachMasterMsgService;


    @GetMapping
    public List<SearchMasterMsg> getAll(){
        return seachMasterMsgService.findAll();
    }

    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg,HttpServletRequest request) {
        try {
            // 拿取登录用户信息
            Logininfo loginInfo = LoginContext.getLoginInfo(request);
            // 向业务层传入寻主信息和登录用户id
            seachMasterMsgService.publish(searchMasterMsg, loginInfo.getId());
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("发布失败！"+e.getMessage());
        }
    }

    // 后端管理人员查看待处理信息
    @PostMapping("/pending")
    public PageList<SearchMasterMsg> pending(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request) {
        try {
            // 拿去登录人信息
            Logininfo loginInfo = LoginContext.getLoginInfo(request);
            // 调用方法传入登录人id和查询条件
            PageList<SearchMasterMsg> list = seachMasterMsgService.pending(query,loginInfo.getId());
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    // 后端管理人员查看已处理信息
    @PostMapping("/processed")
    public PageList<SearchMasterMsg> processed(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request) {
        try {
            // 拿去登录人信息
            Logininfo loginInfo = LoginContext.getLoginInfo(request);
            // 调用方法传入登录人id和查询条件
            PageList<SearchMasterMsg> list = seachMasterMsgService.processed(query,loginInfo.getId());
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    // 用户查看自己的寻主信息





    // 处理宠物 新增详细信息
    @PutMapping("/handle")
    public AjaxResult handle(@RequestBody Pet pet, HttpServletRequest request){
        try {
            Logininfo loginInfo = LoginContext.getLoginInfo(request);
            seachMasterMsgService.handle(pet,loginInfo.getId());
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("处理失败！"+e.getMessage());
        }
    }
}
